package Dao;

import entity.Houseinfo;
import org.apache.ibatis.session.SqlSession;
import tool.MyBatisUtil;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class HouseinfoDaoImpl implements HouseinfoDao{
    SqlSession session =null;
    public List<Houseinfo> getHouseinfo() {
        List<Houseinfo> list =new ArrayList<>();
        try {
           session=MyBatisUtil.getSession();
           list=session.getMapper(HouseinfoDao.class).getHouseinfo();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    public Houseinfo getHouseinfoRecent() {
        Houseinfo houseinfo =new Houseinfo();
        try {
            session=MyBatisUtil.getSession();
            houseinfo=session.getMapper(HouseinfoDao.class).getHouseinfoRecent();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return houseinfo;
    }

    public List<Houseinfo> getHouseinforange(Integer money) {
        List<Houseinfo> list =new ArrayList<>();
        try {
            session=MyBatisUtil.getSession();
            list=session.getMapper(HouseinfoDao.class).getHouseinforange(money);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    public List<Houseinfo> getHouseinfoData(Date date, Date date1) {
        List<Houseinfo> list =new ArrayList<>();
        try {
            session=MyBatisUtil.getSession();
            list=session.getMapper(HouseinfoDao.class).getHouseinfoData(date,date1);
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            MyBatisUtil.closeSession();
        }
        return list;
    }

    public int addHouseinfo(Houseinfo houseinfo) {
        int result=-1;
        try {
            session = MyBatisUtil.getSession();
            //执行数据库操作  （dao接口的全限定名+id）
            result = session.getMapper(HouseinfoDao.class).addHouseinfo(houseinfo);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
            return result;
        } finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    public int delHouseinfo(Date date) {
        int result=-1;
        try {
            session = MyBatisUtil.getSession();
            //执行数据库操作  （dao接口的全限定名+id）
            result = session.getMapper(HouseinfoDao.class).delHouseinfo(date);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
            return result;
        } finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }

    public int updateHouseinfo(Houseinfo houseinfo) {
        int result=-1;
        try {
            session = MyBatisUtil.getSession();
            //执行数据库操作  （dao接口的全限定名+id）
            result = session.getMapper(HouseinfoDao.class).updateHouseinfo(houseinfo);
            session.commit();
        } catch (Exception e) {
            e.printStackTrace();
            session.rollback();
            return result;
        } finally {
            MyBatisUtil.closeSession();
        }
        return result;
    }
}
